package com.appspot.niehonglei.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.niehonglei.dao.PMF;
import com.appspot.niehonglei.jdo.Comment;

public class DeleteComments implements ActionInterface{
	@SuppressWarnings("unchecked")
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		try {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			String querySql = "select from com.appspot.niehonglei.jdo.Comment";
			List<Comment> comments = (List<Comment>) pm.newQuery(querySql).execute();
			for (Comment comment : comments) {
				Calendar c = Calendar.getInstance();
				c.setTime(new Date());
				c.add(Calendar.MONTH, -1);
				System.out.println(comment);
				if (comment.getCreateDate().after(c.getTime())) {
					pm.deletePersistent(comment);
				}
				
			}
			pm.close();
			response.sendRedirect("/comment");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
